package com.example.tang.zhayan.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tang.zhayan.R;
import com.example.tang.zhayan.bean.WorksBodyItem;
import com.squareup.picasso.Picasso;

import org.xutils.x;

import java.util.List;

/**
 * Created by tang on 2015/12/14.
 */
public class WorksBodyAdapter extends BaseAbstractAdapter<WorksBodyItem> {

    //数据类型格式
    private final int TYPE_1 = 0;
    private final int TYPE_2 = 1;

    //定义数据类型的总数
    private final int TYPE_TOTAL = 2;

    public WorksBodyAdapter(Context context, List<WorksBodyItem> list) {
        super(context, list);
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_TOTAL;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderLeft holderLeft = null;
        ViewHolderRight holderRight = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_1:
                    convertView = inflater.inflate(R.layout.works_listview_left, null);
                    holderLeft = new ViewHolderLeft(convertView);
                    convertView.setTag(holderLeft);
                    break;
                case TYPE_2:
                    convertView = inflater.inflate(R.layout.works_listview_right, null);
                    holderRight = new ViewHolderRight(convertView);
                    convertView.setTag(holderRight);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_1:
                    holderLeft = (ViewHolderLeft) convertView.getTag();
                    break;
                case TYPE_2:
                    holderRight = (ViewHolderRight) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case TYPE_1:
                if (!list.get(position).getCover().equals("") && !list.get(position).getStylistImg().equals("")) {
                    Picasso.with(context).load(list.get(position).getCover())
                            .placeholder(R.mipmap.default_loader_image1).into(holderLeft.imgLarge);
                    Picasso.with(context).load(list.get(position).getStylistImg())
                            .placeholder(R.mipmap.default_loader_image1).into(holderLeft.imgSmall);
                }
                holderLeft.txtEngName.setText(list.get(position).getStylistName());
                holderLeft.txtName.setText(list.get(position).getItem_name());
                holderLeft.txtNewPrice.setText("￥"+list.get(position).getNew_prc());
                holderLeft.txtOldPrice.setText("￥"+list.get(position).getOld_prc());
                holderLeft.txtOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                break;
            case TYPE_2:
                if (!list.get(position).getCover().equals("") && !list.get(position).getStylistImg().equals("")) {
                    Picasso.with(context).load(list.get(position).getCover())
                            .placeholder(R.mipmap.default_loader_image1).into(holderRight.imgLarge);
                    Picasso.with(context).load(list.get(position).getStylistImg())
                            .placeholder(R.mipmap.default_loader_image1).into(holderRight.imgSmall);
                }
                holderRight.txtEngName.setText(list.get(position).getStylistName());
                holderRight.txtName.setText(list.get(position).getItem_name());
                holderRight.txtNewPrice.setText("￥"+list.get(position).getNew_prc());
                holderRight.txtOldPrice.setText("￥"+list.get(position).getOld_prc());
                holderRight.txtOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                break;
        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        if ((position % 2) == 1) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    /**
     * 第一种模式
     */
    class ViewHolderLeft {
        ImageView imgLarge;
        ImageView imgSmall;
        TextView txtEngName;
        TextView txtName;
        TextView txtNewPrice;
        TextView txtOldPrice;

        public ViewHolderLeft(View convertView) {
            imgLarge = (ImageView) convertView.findViewById(R.id.img_works_large);
            imgSmall = (ImageView) convertView.findViewById(R.id.img_works_small);
            txtEngName = (TextView) convertView.findViewById(R.id.tv_works_engtxt);
            txtName = (TextView) convertView.findViewById(R.id.tv_works_name);
            txtNewPrice = (TextView) convertView.findViewById(R.id.tv_works_newprice);
            txtOldPrice = (TextView) convertView.findViewById(R.id.tv_works_oldprice);
        }
    }

    /**
     * 第二种模式
     */
    class ViewHolderRight {
        ImageView imgLarge;
        ImageView imgSmall;
        TextView txtEngName;
        TextView txtName;
        TextView txtNewPrice;
        TextView txtOldPrice;

        public ViewHolderRight(View convertView) {
            imgLarge = (ImageView) convertView.findViewById(R.id.img_works_large);
            imgSmall = (ImageView) convertView.findViewById(R.id.img_works_small);
            txtEngName = (TextView) convertView.findViewById(R.id.tv_works_engtxt);
            txtName = (TextView) convertView.findViewById(R.id.tv_works_name);
            txtNewPrice = (TextView) convertView.findViewById(R.id.tv_works_newprice);
            txtOldPrice = (TextView) convertView.findViewById(R.id.tv_works_oldprice);
        }
    }
}
